// packageF/crowd_activity/order/crowd_activity_Order.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoData: [],
    dyFormPopup: false,
    selfPay: false,
    diyform: {
      form_id: "",
      diyform_data_id: "",
      pre_id: "",
    },
    Hei: 0,
    imgCurrent: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '确认订单',
    })
    this.data.options = options;
    if (options.self == 'true') {
      this.setData({
        selfPay: true
      })
    }
    console.log(this.data.selfPay)
    this.getData();
  },
  dyFormPopupClick() {
    this.setData({
      dyFormPopup: true
    })
  },
  showDiyClose() {
    this.setData({
      dyFormPopup: false
    })
  },
  swiperChange(e) {
    let {
      current
    } = e.detail;
    this.setData({
      imgCurrent: current + 1
    })
  },
  traCheckedData(e) {
    this.setData({
      // dyFormPopup: false,
      'diyform.diyform_data_id': e.detail.diyform_data_id
    })
    this.submitTap();
  },
  imgH(e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width;
    var swiperH = winWid * imgh / imgw //等比设置swiper的高度。  即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    console.log(this.data.Hei, swiperH)
    if (this.data.Hei < swiperH) {
      this.setData({
        Hei: swiperH //设置高度
      })
    }

  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.crowdfunding.frontend.pre-activity-order.selfPaidPreOrder");
    app._getNetWork({
      url: urlStr,
      data: {
        activity_id: this.data.options.id
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            infoData: res.data,
            'diyform.form_id': res.data.diy_form_id
          })
          // 随机显示宣言
          var random;
          if (this.data.infoData.supporter_comment.length > 0) {
            if (this.data.infoData.supporter_comment.length <= 1) {
              random = 0
            } else {
              random = Math.floor(Math.random() * this.data.infoData.supporter_comment.length);
            }
            this.setData({
              supporter_comment: this.data.infoData.supporter_comment[random].comment
            })
          }
          this.dyFormPopupClick()
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  async subMethod() {
    let dsfrom = this.selectComponent("#dsfrom");
    await dsfrom.submitForm();
  },
  submitTap() {
    let url = "";
    if (!this.data.diyform.diyform_data_id) {
      app.tips('请填写表单')
      return;
    }
    let json = {
      diy_form_data_id: this.data.diyform.diyform_data_id,
      activity_id: this.data.infoData.id
    };
    if (this.data.options.uid) {
      json.uid = this.data.options.uid;
    }
    if (this.data.selfPay) {
      // 自付接口
      json.diy_form_id = this.data.infoData.diy_form_id;
      url = app.getNetAddresss("plugin.crowdfunding.frontend.pre-activity-order.joinSelfPaidActivity");
    } else {
      url = app.getNetAddresss("plugin.crowdfunding.frontend.activity-member.joinCrowdfundingActivity");
    }
    app._getNetWork({
      url: url,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          if (this.data.selfPay) {
            wx.navigateTo({
              url: '/packageF/crowd_activity/crowd_activity_OrderPay/crowd_activity_OrderPay?sn=' + res.data.order_sn,
            })
          } else {
            wx.showToast({
              title: res.msg,
              duration: 1000,
              icon: 'none',
              success: (resdata) => {
                setTimeout(() => {
                  wx.redirectTo({
                    url: '/packageF/crowd_activity/funding/funding?id=' + res.data.activity_member_id,
                  })
                }, 1000);
              }
            })
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

})